package com.nidu.demo.mail.executor.query;

import com.alibaba.cola.dto.PageResponse;
import com.nidu.demo.mail.convertor.MailLogConvertor;
import com.nidu.demo.mail.convertor.MailLogCriteriaConvertor;
import com.nidu.demo.mail.dto.MailLogCO;
import com.nidu.demo.mail.dto.MailLogPageQry;
import com.nidu.demo.mail.gateway.MailLogGateway;
import com.nidu.demo.mail.model.MailLog;
import com.nidu.demo.mail.model.MailLogCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 邮件发送日志分页查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MailLogPageQryExe {

    private final MailLogGateway gateway;
    private final MailLogConvertor convertor;
    private final MailLogCriteriaConvertor criteriaConvertor;

    public PageResponse<MailLogCO> execute(MailLogPageQry qry) {
        // 1. 转换查询条件
        MailLogCriteria criteria = criteriaConvertor.toDataObject(qry);

        // 2. 执行分页查询
        PageResponse<MailLog> page = gateway.pageByCondition(criteria);

        // 3. 转换为DTO并返回
        List<MailLogCO> mailLogCOS = convertor.toClientObjects(page.getData());
        return PageResponse.of(mailLogCOS, page.getTotalCount(), page.getPageSize(), page.getPageIndex());
    }

}
